package com.sljy.hzk.ui.detection;

import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.Log;
import android.view.View;
import android.view.ViewParent;
import android.widget.ImageButton;
import android.widget.LinearLayout;

import com.sljy.hzk.R;
import com.sljy.hzk.annotation.ActivityInfo;
import com.sljy.hzk.base.BaseActivity;
import com.sljy.hzk.base.UserInfo;
import com.sljy.hzk.entity.DetectionPartitionEntity;
import com.sljy.hzk.net.BaseCallBack;
import com.sljy.hzk.net.HttpHeper;
import com.sljy.hzk.net.service.DetectionService;
import com.sljy.hzk.room.AppDataBase;
import com.tkk.api.RxEventProcessor;
import com.tkk.api.annotation.Recipient;
import com.trello.rxlifecycle2.android.ActivityEvent;
import com.unnamed.b.atv.model.TreeNode;
import com.unnamed.b.atv.view.AndroidTreeView;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

@ActivityInfo(layout = R.layout.activity_chose_partition, isShowActionBar = true, title = "选择分区", rightIcRes = R.drawable.homepage)
public class ChosePartitionActivity extends BaseActivity {
    @BindView(R.id.container)
    LinearLayout containerLayout;
    private AndroidTreeView tView;
    TreeNode treeRoot = TreeNode.root();
    TreeNode checkNode;
    @BindView(R.id.tv_sure)
    ImageButton tv_sure;
    @BindView(R.id.refresh)
    SwipeRefreshLayout refresh;
    @Override
    protected void init() {
        RxEventProcessor.get().bind(this);
        refresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener(){
            @Override
            public void onRefresh() {
                refresh.setRefreshing(false);
                onRefreshToast();
                loadData();
            }
        });
        loadData();
    }

    private void loadData(){
        HttpHeper.get().create(DetectionService.class)
                .getAllDetectionPartition(UserInfo.getUserInfo().getComPId())
                .compose(this.bindUntilEvent(ActivityEvent.DESTROY))
                .compose(getThread())
                .subscribe(new BaseCallBack<DetectionPartitionEntity>(this) {
                    @Override
                    public void onCallBackSuccess(DetectionPartitionEntity data) {
                        initTreeView(data);
                    }
                    @Override
                    public void onFail(String mes) {
                        showToast(mes);
                    }
                });
    }

    /**
     * 先序遍历 根-左-右
     *
     * @param root
     */
    public void preOrder(DetectionPartitionEntity root, TreeNode folder, int depth) {
        if (root.isHaveChildren()) {
            depth++;
            String n ;
            for (DetectionPartitionEntity child : root.getChildren()) {
                n = child.getpName();
                if(child!= null & child.getChildren().size() > 0) n = n+"<font color='red'>*</font>";
                folder.addChild(new TreeNode(new SelectableItemHolder.Item(n, child.getpId(),child.getAllName())).setViewHolder(new SelectableItemHolder(this, depth)));
                preOrder(child, folder, depth);
            }
        }
    }

    /**
     * 后根遍历  根--左右的子节点
     *
     * @param root 树的根结点
     */
    public void postOrder(DetectionPartitionEntity root) {
        String n;
        for (DetectionPartitionEntity child : root.getChildren()) {
            n = child.getpName();
            if(child!= null & child.getChildren().size() > 0) n = n+"<font color='red'>*</font>";
            TreeNode folder = new TreeNode(new SelectableItemHolder.Item(n, child.getpId(),child.getAllName())).setViewHolder(new SelectableItemHolder(this, 1));
            if (child != null) {
                preOrder(child, folder, 1);
            }
            treeRoot.addChild(folder);
        }
    }

    /**
     * 广度优先遍历--按照深度遍历
     *
     * @param root
     */
    public void gdOrder(DetectionPartitionEntity root) {
        for (DetectionPartitionEntity child : root.getChildren()) {
            Log.d(TAG, ">>>广度优先---" + child.getpId());
        }
        for (DetectionPartitionEntity child : root.getChildren()) {
            gdOrder(child);
        }
    }

    private void initTreeView(DetectionPartitionEntity data) {
        if(tView != null){
            tView.removeNode(treeRoot);
            treeRoot = TreeNode.root();
            postOrder(data);
            tView.setRoot(treeRoot);
            tView.deselectAll();
            return;
        }
        postOrder(data);
        tView = new AndroidTreeView(this, treeRoot);
        tView.setSelectionModeEnabled(true);
        tView.setDefaultAnimation(true);
        containerLayout.addView(tView.getView());
        tv_sure.setVisibility(View.VISIBLE);
    }

    /**
     * 实现单选
     * @param node
     */
    @Recipient(tag = "isChecked")
    public void onNodeCk(TreeNode node) {
        if (checkNode != null && checkNode.getId() != node.getId()) {
            tView.selectNode(checkNode,false);
            checkNode.setSelected(false);
        }
        checkNode = node;
    }

    @OnClick(R.id.tv_sure)
    public void sure(){
        if (checkNode == null){
            showToast("请选择分区");
            return;
        }
        SelectableItemHolder.Item item = (SelectableItemHolder.Item) checkNode.getValue();
        RxEventProcessor.get().post("chose_finish",item.getpId(),item.getpName(),item.getAllName());
        finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        RxEventProcessor.get().unBind(this);
    }
}
